package com.ihighest.app.infox.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.SelectLableAdapter;
import com.ihighest.app.infox.myview.Tag;
import com.ihighest.app.infox.tools.CheckNetWork;

/**
 * 选择标签
 * 
 * 返回tags集合 返回码 RESULT_OK 取消返回 RESULT_CANCELED
 * 
 * intent.putExtra("tags", (Serializable) adapter.getTags());
 * 
 * @author yyh Created at 2015-10-21 下午6:18:51
 * @version 1.0
 */
public class SelectLableActivity extends BaseActivity {

	private ListView lisView;
	private SelectLableAdapter adapter;
	public static int a;
	ArrayList<String> list;

	List<Tag> tags;
	private ImageView close;
	private Button sure;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_selectlable);
		list = getIntent().getStringArrayListExtra(Const.STEPLIST);
		tags = new ArrayList<Tag>();
		initView();
	}

	/**
	 * 初始化绑定
	 * 
	 * 
	 * @author lkx Created at 2015-10-15 下午2:49:38
	 */
	private void initView() {

		lisView = (ListView) findViewById(R.id.lableListview);
		initData();
		lisView.setAdapter(adapter);

		close = (ImageView) findViewById(R.id.id_close);
		sure = (Button) findViewById(R.id.id_sure);

		close.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				SelectLableActivity.this.setResult(RESULT_CANCELED);
				SelectLableActivity.this.finish();
			}
		});
		sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.putExtra("tags", (Serializable) adapter.getTags());
				SelectLableActivity.this.setResult(RESULT_OK, intent);
				finish();
			}
		});

	}

	/**
	 * 获取标签数据
	 * 
	 * 
	 * @author lkx Created at 2015-10-15 下午7:36:52
	 */
	private void initData() {
		if (CheckNetWork.isConnected(this)) {
			// 如果网络正常
			Api.data("tag_list.json", new ApiCallback(this, true) {

				@Override
				public void handleSuccess(ApiResult result) {
					if (adapter == null) {
						adapter = new SelectLableAdapter(SelectLableActivity.this, result.getDatas(), tags);
					}
					lisView.setAdapter(adapter);
					adapter.notifyDataSetChanged();
				}

				@Override
				public void handleFailure(Exception e, String msg) {

				}
			}, this);

		}

	}
}
